﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class NewsRelatedDao : AbstractDao<NewsRelated>, INewsRelatedDao
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">Instance of NewsRelatedKey class.</param>
        /// <returns></returns>
        public override NewsRelated
        getById(object id)
        {
            try
            {
                if (!id.GetType().Equals(typeof(NewsRelatedKey)))
                {
                    //throw exception that notice about invalid type of param.
                    return null;
                }

                var k = (NewsRelatedKey)id;
                return getSingle(i => i.newsId == k.NewId && i.newsRelatedId == k.NewsRelatedId);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public NewsRelated
        getById(int newsId, int newsRelatedId)
        {
            return getSingle(i => i.newsId == newsId && i.newsRelatedId == newsRelatedId);
        }

        public List<NewsRelated>
        getByNews(int newsId)
        {
            try
            {
                return getMultiple(i => i.newsId == newsId);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<NewsRelated>
        getByNewsRelated(int newsRelatedId)
        {
            try
            {
                return getMultiple(i => i.newsRelatedId == newsRelatedId);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region INewsRelatedDao

    public interface INewsRelatedDao : IDao<NewsRelated>
    {
        NewsRelated getById(int newsId, int newsRelatedId);

        List<NewsRelated> getByNews(int newsId);

        List<NewsRelated> getByNewsRelated(int newsRelatedId);
    }
    #endregion
}